package com.airnewslinkosky.ticket.ui.lowerlevel;

import java.util.ArrayList;
import java.util.List;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

import com.airnewslinkosky.ticket.R;
import com.airnewslinkosky.ticket.model.CItem;
import com.airnewslinkosky.ticket.model.OfficeInfo;
import com.airnewslinkosky.ticket.model.OfficeResponseResult;
import com.airnewslinkosky.ticket.model.ProfitRule;
import com.airnewslinkosky.ticket.model.ProfitRuleResult;
import com.airnewslinkosky.ticket.net.WebAccessor;
import com.airnewslinkosky.ticket.pay.util.BaseHelper;
import com.airnewslinkosky.ticket.ui.BaseActivity;
import com.airnewslinkosky.ticket.util.Constants;
import com.mobclick.android.MobclickAgent;

public class LowerLevelTabAdd  extends BaseActivity implements OnClickListener{

	private boolean isCheck = true;
	private Button statusBut;
	private Button lowerLevelButton;
	
	private Spinner profitAccount;
	private String profitAccountSelect = "";
	
	private String status = "0";
	
	//BSP机票留存，外放的显示层
	private RelativeLayout bsp_jipiao_liuchun_marker ,bsp_jipiao_waifang_marker;
	//BSP机票留存，外放的按钮
	private Button bsp_jipiao_liuchun,bsp_jipiao_waifang;
	//BSP机票是否外放
	private boolean isWaiFangCheck = true;
	//BSP机票是否外放
	private boolean isLiuChunCheck = true;
	private String liuChunCheckStatus = "0";
	//留存本地百分比
	private String bsp_jipiao_liuChunLocalPersent = "";
	//留存异地百分比
	private String bsp_jipiao_liuChunNonlocalPersent = "";
	//外放本地金额
	private String bsp_jipiao_waiFangLocalAmount = "";
	//外放异地金额
	private String bsp_jipiao_waiFangNonlocalAmount = "";
	
	//区域设置
	private EditText bsp_jipiao_liuchun_local_0_edit,bsp_jipiao_liuchun_local_48_edit;
	private EditText bsp_jipiao_liuchun_local_48_b_edit,bsp_jipiao_liuchun_local_49_edit;
	private EditText bsp_jipiao_liuchun_local_49_b_edit,bsp_jipiao_liuchun_local_50_edit;
	private EditText bsp_jipiao_liuchun_local_50_b_edit,bsp_jipiao_liuchun_local_100_edit;
	//值
	private EditText bsp_jipiao_liuchun_local_0_48_edit,bsp_jipiao_liuchun_waidi_0_48_edit;
	private EditText bsp_jipiao_liuchun_local_48_49_edit,bsp_jipiao_liuchun_waidi_48_49_edit;
	private EditText bsp_jipiao_liuchun_local_49_50_edit,bsp_jipiao_liuchun_waidi_49_50_edit;
	private EditText bsp_jipiao_liuchun_local_50_100_edit,bsp_jipiao_liuchun_waidi_50_100_edit;
	private EditText bsp_jipiao_waifang_local_edit,bsp_jipiao_waifang_waidi_edit;
	
	//B2B本票留存，外放的显示层
	private RelativeLayout b2b_benpiao_jipiao_liuchun_marker ,b2b_benpiao_jipiao_waifang_marker;
	//B2B本票留存，外放的按钮
	private Button b2b_benpiao_jipiao_liuchun,b2b_benpiao_jipiao_waifang;
	//B2B本票是否外放
	private boolean isWaiFangCheckTejia = true;
	//B2B本票是否外放
	private boolean isLiuChunCheckTejia = true;
	private String liuChunCheckTejiaStatus = "0";
	//留存本地百分比
	private String b2b_benpiao_liuChunLocalPersent = "";
	//留存异地百分比
	private String b2b_benpiao_liuChunNonlocalPersent = "";
	//外放本地金额
	private String b2b_benpiao_waiFangLocalAmount = "";
	//外放异地金额
	private String b2b_benpiao_waiFangNonlocalAmount = "";
	
	//区域设置
	private EditText b2b_benpiao_jipiao_liuchun_0_edit,b2b_benpiao_jipiao_liuchun_48_edit;
	private EditText b2b_benpiao_jipiao_liuchun_48_b_edit,b2b_benpiao_jipiao_liuchun_49_edit;
	private EditText b2b_benpiao_jipiao_liuchun_local_49_b_edit,b2b_benpiao_jipiao_liuchun_local_50_edit;
	private EditText b2b_benpiao_jipiao_liuchun_local_50_b_edit,b2b_benpiao_jipiao_liuchun_local_100_edit;
	
	private EditText b2b_benpiao_jipiao_liuchun_local_0_48_edit,b2b_benpiao_jipiao_liuchun_waidi_0_48_edit;
	private EditText b2b_benpiao_jipiao_liuchun_local_48_49_edit,b2b_benpiao_jipiao_liuchun_waidi_48_49_edit;
	private EditText b2b_benpiao_jipiao_liuchun_local_49_50_edit,b2b_benpiao_jipiao_liuchun_waidi_49_50_edit;
	private EditText b2b_benpiao_jipiao_liuchun_local_50_100_edit,b2b_benpiao_jipiao_liuchun_waidi_50_100_edit;
	private EditText b2b_benpiao_jipiao_waifang_local_edit,b2b_benpiao_jipiao_waifang_waidi_edit;
	
	//特价政策机票留存，外放的显示层
	private RelativeLayout tejia_zc_jipiao_liuchun_marker ,tejia_zc_jipiao_waifang_marker;
	//特价政策机票留存，外放的按钮
	private Button tejia_zc_jipiao_liuchun,tejia_zc_jipiao_waifang;
	//特价政策机票是否外放
	private boolean isWaiFangCheckTejiaZc = true;
	//特价政策机票是否外放
	private boolean isLiuChunCheckTejiaZc = true;
	private String liuChunCheckTejiaZcStatus = "0";
	//留存本地百分比
	private String tejia_zc_jipiao_liuChunLocalPersent = "";
	//留存异地百分比
	private String tejia_zc_jipiao_liuChunNonlocalPersent = "";
	//外放本地金额
	private String tejia_zc_jipiao_waiFangLocalAmount = "";
	//外放异地金额
	private String tejia_zc_jipiao_waiFangNonlocalAmount = "";
	
	//区域设置
	private EditText tejia_zc_jipiao_liuchun_local_0_edit,tejia_zc_jipiao_liuchun_local_48_edit;
	private EditText tejia_zc_jipiao_liuchun_local_48_b_edit,tejia_zc_jipiao_liuchun_local_49_edit;
	private EditText tejia_zc_jipiao_liuchun_local_49_b_edit,tejia_zc_jipiao_liuchun_local_50_edit;
	private EditText tejia_zc_jipiao_liuchun_local_50_b_edit,tejia_zc_jipiao_liuchun_local_100_edit;
	
	private EditText tejia_zc_jipiao_liuchun_local_0_48_edit,tejia_zc_jipiao_liuchun_waidi_0_48_edit;
	private EditText tejia_zc_jipiao_liuchun_local_48_49_edit,tejia_zc_jipiao_liuchun_waidi_48_49_edit;
	private EditText tejia_zc_jipiao_liuchun_local_49_50_edit,tejia_zc_jipiao_liuchun_waidi_49_50_edit;
	private EditText tejia_zc_jipiao_liuchun_local_50_100_edit,tejia_zc_jipiao_liuchun_waidi_50_100_edit;
	private EditText tejia_zc_jipiao_waifang_local_edit,tejia_zc_jipiao_waifang_waidi_edit;
	
	private List<ProfitRule> profitRuleList;//制定的规则LIST
	private boolean isCheckRight =  true;//检查是否输入数据正确
	private String dirOfficeID = "";//目标商户号
	
	private ProgressDialog progressDialog = null;
	public Handler myHandler;
	public final int NORESULT = 6;
	public final int ORTHERRESULT =5;
	public final int SEARCH_ERROR=7;
	public final int USER_ERROR = 1;
	public final int PWD_ERROR = 2;
	public final int MOBILE_ERROR = 3;
	public final int EMAIL_ERROR = 4;
	public final int INPUT_ERROR = 9;
	public final int SETVALUE_ERROR = 10;
	public final int SUCCESS = 0;
	
	private SharedPreferences sp;
	
	//分销商名称,联系人手机,联系人邮箱
	private EditText distributor,mobile,email;
	//管理员名称,管理员密码
	private EditText administrator,password;
	
	private Toast toast;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lower_level_tab_edit);
		// 删除窗口背景
        getWindow().setBackgroundDrawable(null);
        
		activityList.add(this);
		
		findViewById(R.id.backBut).setOnClickListener(this);
		
		TabHost tabHost = (TabHost)this.findViewById(R.id.tabHost01);
        tabHost.setup();
        
        tabHost.addTab(tabHost.newTabSpec("BSP机票").setIndicator("BSP机票").setContent(R.id.view1));   
        tabHost.addTab(tabHost.newTabSpec("B2B本票").setIndicator("B2B本票").setContent(R.id.view2));   
        tabHost.addTab(tabHost.newTabSpec("特价政策机票").setIndicator("特价政策机票").setContent(R.id.view3));  
        TabWidget tabWidget = tabHost.getTabWidget();
        for (int i =0; i < tabWidget.getChildCount(); i++) {  
            tabWidget.getChildAt(i).getLayoutParams().height = 45;  
        }
        
		initOfficer();
		initEditValue();
//		initProfitAccount();
		
		myHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case SUCCESS :
					toast = Toast.makeText(LowerLevelTabAdd.this, "成功注册分销商信息", Toast.LENGTH_LONG);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
					break;
				case NORESULT:
					BaseHelper.showDialog(LowerLevelTabAdd.this, "提示", "未能成功注册分销商信息",
    						R.drawable.infoicon);
					break;
				case SEARCH_ERROR:
					toast = Toast.makeText(LowerLevelTabAdd.this, (String)msg.obj, Toast.LENGTH_LONG);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
					break;
				case ORTHERRESULT:
					toast = Toast.makeText(LowerLevelTabAdd.this, "网络超时或者其他错误", Toast.LENGTH_LONG);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
					break;
				case INPUT_ERROR:
					BaseHelper.showDialog(LowerLevelTabAdd.this, "提示", "请填写完整信息",
    						R.drawable.infoicon);
					break;
				case USER_ERROR:
					toast = Toast.makeText(LowerLevelTabAdd.this, "姓名应为2-6个中文字符或2-20个英文", Toast.LENGTH_LONG);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
					break;
				case SETVALUE_ERROR:
//					Toast.makeText(LowerLevelTabAdd.this, "规则信息记录必须填写完整！", Toast.LENGTH_LONG).show();
					BaseHelper.showDialog(LowerLevelTabAdd.this, "提示", "规则信息记录必须填写完整",
    						R.drawable.infoicon);
					break;
				case MOBILE_ERROR:
					toast = Toast.makeText(LowerLevelTabAdd.this, "手机号填写不正确", Toast.LENGTH_LONG);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
					break;
				case EMAIL_ERROR:
					toast = Toast.makeText(LowerLevelTabAdd.this, "Email填写不正确", Toast.LENGTH_LONG);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
					break;
				}
			}
		};
	}
	
	private void initProfitAccount() {
		List<CItem> lst = new ArrayList<CItem>();
		CItem  itemone = new CItem ("1","支付宝");
//		CItem  itemtwo = new CItem ("2","环迅");
		lst.add(itemone);
		
		//下拉列表
		profitAccount = (Spinner) findViewById(R.id.profitAccount);
		
		ArrayAdapter<CItem> adapterItem = new ArrayAdapter<CItem>(LowerLevelTabAdd.this,
			    android.R.layout.simple_spinner_item, lst);
		adapterItem.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		profitAccount.setAdapter(adapterItem);

		profitAccount.setOnItemSelectedListener(new OnItemSelectedListener() {
    		public void onItemSelected(AdapterView<?> parent,View view,int position,long id) {
    			profitAccountSelect = ((CItem)profitAccount.getSelectedItem()).getId();
    		}
    		public void onNothingSelected(AdapterView<?> parent) {
    		}
        });
	}
	
	//初始化分销商基础数据
	private void initOfficer() {
		distributor =  (EditText) findViewById(R.id.distributor);
		mobile =  (EditText) findViewById(R.id.mobile);
		email =  (EditText) findViewById(R.id.email);
		administrator =  (EditText) findViewById(R.id.administrator);
		password =  (EditText) findViewById(R.id.password);
		
		statusBut =  (Button) findViewById(R.id.statusBut);
		statusBut.setOnClickListener(this);
		
		lowerLevelButton =  (Button) findViewById(R.id.lowerLevelButton);
		lowerLevelButton.setOnClickListener(this);
		
		bsp_jipiao_liuchun_marker =  (RelativeLayout) findViewById(R.id.bsp_jipiao_liuchun_marker);
		bsp_jipiao_waifang_marker =  (RelativeLayout) findViewById(R.id.bsp_jipiao_waifang_marker);
		bsp_jipiao_liuchun =  (Button) findViewById(R.id.bsp_jipiao_liuchun);
		bsp_jipiao_liuchun.setOnClickListener(this);
		bsp_jipiao_waifang =  (Button) findViewById(R.id.bsp_jipiao_waifang);
		bsp_jipiao_waifang.setOnClickListener(this);
		
		b2b_benpiao_jipiao_liuchun_marker =  (RelativeLayout) findViewById(R.id.b2b_benpiao_jipiao_liuchun_marker);
		b2b_benpiao_jipiao_waifang_marker =  (RelativeLayout) findViewById(R.id.b2b_benpiao_jipiao_waifang_marker);
		b2b_benpiao_jipiao_liuchun =  (Button) findViewById(R.id.b2b_benpiao_jipiao_liuchun);
		b2b_benpiao_jipiao_liuchun.setOnClickListener(this);
		b2b_benpiao_jipiao_waifang =  (Button) findViewById(R.id.b2b_benpiao_jipiao_waifang);
		b2b_benpiao_jipiao_waifang.setOnClickListener(this);
		
		tejia_zc_jipiao_liuchun_marker =  (RelativeLayout) findViewById(R.id.tejia_zc_jipiao_liuchun_marker);
		tejia_zc_jipiao_waifang_marker =  (RelativeLayout) findViewById(R.id.tejia_zc_jipiao_waifang_marker);
		tejia_zc_jipiao_liuchun =  (Button) findViewById(R.id.tejia_zc_jipiao_liuchun);
		tejia_zc_jipiao_liuchun.setOnClickListener(this);
		tejia_zc_jipiao_waifang =  (Button) findViewById(R.id.tejia_zc_jipiao_waifang);
		tejia_zc_jipiao_waifang.setOnClickListener(this);
	}
	
	private void initEditValue() {
		
		bsp_jipiao_liuchun_local_0_edit = (EditText) findViewById(R.id.bsp_jipiao_liuchun_local_0_edit);
		bsp_jipiao_liuchun_local_48_edit = (EditText) findViewById(R.id.bsp_jipiao_liuchun_local_48_edit);
		bsp_jipiao_liuchun_local_48_b_edit = (EditText) findViewById(R.id.bsp_jipiao_liuchun_local_48_b_edit);
		bsp_jipiao_liuchun_local_49_edit = (EditText) findViewById(R.id.bsp_jipiao_liuchun_local_49_edit);
		bsp_jipiao_liuchun_local_49_b_edit = (EditText) findViewById(R.id.bsp_jipiao_liuchun_local_49_b_edit);
		bsp_jipiao_liuchun_local_50_edit = (EditText) findViewById(R.id.bsp_jipiao_liuchun_local_50_edit);
		bsp_jipiao_liuchun_local_50_b_edit = (EditText) findViewById(R.id.bsp_jipiao_liuchun_local_50_b_edit);
		bsp_jipiao_liuchun_local_100_edit = (EditText) findViewById(R.id.bsp_jipiao_liuchun_local_100_edit);
		
		bsp_jipiao_liuchun_local_0_48_edit = (EditText) findViewById(R.id.bsp_jipiao_liuchun_local_0_48_edit);
		bsp_jipiao_liuchun_waidi_0_48_edit = (EditText) findViewById(R.id.bsp_jipiao_liuchun_waidi_0_48_edit);
		bsp_jipiao_liuchun_local_48_49_edit = (EditText) findViewById(R.id.bsp_jipiao_liuchun_local_48_49_edit);
		bsp_jipiao_liuchun_waidi_48_49_edit = (EditText) findViewById(R.id.bsp_jipiao_liuchun_waidi_48_49_edit);
		bsp_jipiao_liuchun_local_49_50_edit = (EditText) findViewById(R.id.bsp_jipiao_liuchun_local_49_50_edit);
		bsp_jipiao_liuchun_waidi_49_50_edit = (EditText) findViewById(R.id.bsp_jipiao_liuchun_waidi_49_50_edit);
		bsp_jipiao_liuchun_local_50_100_edit = (EditText) findViewById(R.id.bsp_jipiao_liuchun_local_50_100_edit);
		bsp_jipiao_liuchun_waidi_50_100_edit = (EditText) findViewById(R.id.bsp_jipiao_liuchun_waidi_50_100_edit);
		bsp_jipiao_waifang_local_edit = (EditText) findViewById(R.id.bsp_jipiao_waifang_local_edit);
		bsp_jipiao_waifang_waidi_edit = (EditText) findViewById(R.id.bsp_jipiao_waifang_waidi_edit);
		
		
		b2b_benpiao_jipiao_liuchun_0_edit = (EditText) findViewById(R.id.b2b_benpiao_jipiao_liuchun_0_edit);
		b2b_benpiao_jipiao_liuchun_48_edit = (EditText) findViewById(R.id.b2b_benpiao_jipiao_liuchun_48_edit);
		b2b_benpiao_jipiao_liuchun_48_b_edit = (EditText) findViewById(R.id.b2b_benpiao_jipiao_liuchun_48_b_edit);
		b2b_benpiao_jipiao_liuchun_49_edit = (EditText) findViewById(R.id.b2b_benpiao_jipiao_liuchun_49_edit);
		b2b_benpiao_jipiao_liuchun_local_49_b_edit = (EditText) findViewById(R.id.b2b_benpiao_jipiao_liuchun_local_49_b_edit);
		b2b_benpiao_jipiao_liuchun_local_50_edit = (EditText) findViewById(R.id.b2b_benpiao_jipiao_liuchun_local_50_edit);
		b2b_benpiao_jipiao_liuchun_local_50_b_edit = (EditText) findViewById(R.id.b2b_benpiao_jipiao_liuchun_local_50_b_edit);
		b2b_benpiao_jipiao_liuchun_local_100_edit = (EditText) findViewById(R.id.b2b_benpiao_jipiao_liuchun_local_100_edit);
		
		b2b_benpiao_jipiao_liuchun_local_0_48_edit = (EditText) findViewById(R.id.b2b_benpiao_jipiao_liuchun_local_0_48_edit);
		b2b_benpiao_jipiao_liuchun_waidi_0_48_edit = (EditText) findViewById(R.id.b2b_benpiao_jipiao_liuchun_waidi_0_48_edit);
		b2b_benpiao_jipiao_liuchun_local_48_49_edit = (EditText) findViewById(R.id.b2b_benpiao_jipiao_liuchun_local_48_49_edit);
		b2b_benpiao_jipiao_liuchun_waidi_48_49_edit = (EditText) findViewById(R.id.b2b_benpiao_jipiao_liuchun_waidi_48_49_edit);
		b2b_benpiao_jipiao_liuchun_local_49_50_edit = (EditText) findViewById(R.id.b2b_benpiao_jipiao_liuchun_local_49_50_edit);
		b2b_benpiao_jipiao_liuchun_waidi_49_50_edit = (EditText) findViewById(R.id.b2b_benpiao_jipiao_liuchun_waidi_49_50_edit);
		b2b_benpiao_jipiao_liuchun_local_50_100_edit = (EditText) findViewById(R.id.b2b_benpiao_jipiao_liuchun_local_50_100_edit);
		b2b_benpiao_jipiao_liuchun_waidi_50_100_edit = (EditText) findViewById(R.id.b2b_benpiao_jipiao_liuchun_waidi_50_100_edit);
		b2b_benpiao_jipiao_waifang_local_edit = (EditText) findViewById(R.id.b2b_benpiao_jipiao_waifang_local_edit);
		b2b_benpiao_jipiao_waifang_waidi_edit = (EditText) findViewById(R.id.b2b_benpiao_jipiao_waifang_waidi_edit);
		
		tejia_zc_jipiao_liuchun_local_0_edit = (EditText) findViewById(R.id.tejia_zc_jipiao_liuchun_local_0_edit);
		tejia_zc_jipiao_liuchun_local_48_edit = (EditText) findViewById(R.id.tejia_zc_jipiao_liuchun_local_48_edit);
		tejia_zc_jipiao_liuchun_local_48_b_edit = (EditText) findViewById(R.id.tejia_zc_jipiao_liuchun_local_48_b_edit);
		tejia_zc_jipiao_liuchun_local_49_edit = (EditText) findViewById(R.id.tejia_zc_jipiao_liuchun_local_49_edit);
		tejia_zc_jipiao_liuchun_local_49_b_edit = (EditText) findViewById(R.id.tejia_zc_jipiao_liuchun_local_49_b_edit);
		tejia_zc_jipiao_liuchun_local_50_edit = (EditText) findViewById(R.id.tejia_zc_jipiao_liuchun_local_50_edit);
		tejia_zc_jipiao_liuchun_local_50_b_edit = (EditText) findViewById(R.id.tejia_zc_jipiao_liuchun_local_50_b_edit);
		tejia_zc_jipiao_liuchun_local_100_edit = (EditText) findViewById(R.id.tejia_zc_jipiao_liuchun_local_100_edit);
		
		
		tejia_zc_jipiao_liuchun_local_0_48_edit = (EditText) findViewById(R.id.tejia_zc_jipiao_liuchun_local_0_48_edit);
		tejia_zc_jipiao_liuchun_waidi_0_48_edit = (EditText) findViewById(R.id.tejia_zc_jipiao_liuchun_waidi_0_48_edit);
		tejia_zc_jipiao_liuchun_local_48_49_edit = (EditText) findViewById(R.id.tejia_zc_jipiao_liuchun_local_48_49_edit);
		tejia_zc_jipiao_liuchun_waidi_48_49_edit = (EditText) findViewById(R.id.tejia_zc_jipiao_liuchun_waidi_48_49_edit);
		tejia_zc_jipiao_liuchun_local_49_50_edit = (EditText) findViewById(R.id.tejia_zc_jipiao_liuchun_local_49_50_edit);
		tejia_zc_jipiao_liuchun_waidi_49_50_edit = (EditText) findViewById(R.id.tejia_zc_jipiao_liuchun_waidi_49_50_edit);
		tejia_zc_jipiao_liuchun_local_50_100_edit = (EditText) findViewById(R.id.tejia_zc_jipiao_liuchun_local_50_100_edit);
		tejia_zc_jipiao_liuchun_waidi_50_100_edit = (EditText) findViewById(R.id.tejia_zc_jipiao_liuchun_waidi_50_100_edit);
		tejia_zc_jipiao_waifang_local_edit = (EditText) findViewById(R.id.tejia_zc_jipiao_waifang_local_edit);
		tejia_zc_jipiao_waifang_waidi_edit = (EditText) findViewById(R.id.tejia_zc_jipiao_waifang_waidi_edit);
	}
	
	private void checkEditValue() {
		if(liuChunCheckStatus.equals("0")){
			bsp_jipiao_liuChunLocalPersent = String.valueOf(bsp_jipiao_liuchun_local_0_48_edit.getText()) + "|" +  String.valueOf(bsp_jipiao_liuchun_local_48_49_edit.getText()) + "|" + String.valueOf(bsp_jipiao_liuchun_local_49_50_edit.getText()) + "|" + String.valueOf(bsp_jipiao_liuchun_local_50_100_edit.getText());
			bsp_jipiao_liuChunNonlocalPersent =  String.valueOf(bsp_jipiao_liuchun_waidi_0_48_edit.getText()) + "|" +  String.valueOf(bsp_jipiao_liuchun_waidi_48_49_edit.getText()) + "|" + String.valueOf(bsp_jipiao_liuchun_waidi_49_50_edit.getText()) + "|" + String.valueOf(bsp_jipiao_liuchun_waidi_50_100_edit.getText());
		} else if(liuChunCheckStatus.equals("1")) {
			bsp_jipiao_waiFangLocalAmount = String.valueOf(bsp_jipiao_waifang_local_edit.getText());
			bsp_jipiao_waiFangNonlocalAmount = String.valueOf(bsp_jipiao_waifang_waidi_edit.getText());
		}
		
		if(liuChunCheckTejiaStatus.equals("0")){
			b2b_benpiao_liuChunLocalPersent = String.valueOf(b2b_benpiao_jipiao_liuchun_local_0_48_edit.getText()) + "|" +  String.valueOf(b2b_benpiao_jipiao_liuchun_local_48_49_edit.getText()) + "|" + String.valueOf(b2b_benpiao_jipiao_liuchun_local_49_50_edit.getText()) + "|" + String.valueOf(b2b_benpiao_jipiao_liuchun_local_50_100_edit.getText());
			b2b_benpiao_liuChunNonlocalPersent =  String.valueOf(b2b_benpiao_jipiao_liuchun_waidi_0_48_edit.getText()) + "|" +  String.valueOf(b2b_benpiao_jipiao_liuchun_waidi_48_49_edit.getText()) + "|" + String.valueOf(b2b_benpiao_jipiao_liuchun_waidi_49_50_edit.getText()) + "|" + String.valueOf(b2b_benpiao_jipiao_liuchun_waidi_50_100_edit.getText());
		} else if(liuChunCheckTejiaStatus.equals("1")) {
			b2b_benpiao_waiFangLocalAmount = String.valueOf(b2b_benpiao_jipiao_waifang_local_edit.getText());
			b2b_benpiao_waiFangNonlocalAmount = String.valueOf(b2b_benpiao_jipiao_waifang_waidi_edit.getText());
		}
		
		if(liuChunCheckTejiaZcStatus.equals("0")){
			tejia_zc_jipiao_liuChunLocalPersent = String.valueOf(tejia_zc_jipiao_liuchun_local_0_48_edit.getText()) + "|" +  String.valueOf(tejia_zc_jipiao_liuchun_local_48_49_edit.getText()) + "|" + String.valueOf(tejia_zc_jipiao_liuchun_local_49_50_edit.getText()) + "|" + String.valueOf(tejia_zc_jipiao_liuchun_local_50_100_edit.getText());
			tejia_zc_jipiao_liuChunNonlocalPersent =  String.valueOf(tejia_zc_jipiao_liuchun_waidi_0_48_edit.getText()) + "|" +  String.valueOf(tejia_zc_jipiao_liuchun_waidi_48_49_edit.getText()) + "|" + String.valueOf(tejia_zc_jipiao_liuchun_waidi_49_50_edit.getText()) + "|" + String.valueOf(tejia_zc_jipiao_liuchun_waidi_50_100_edit.getText());
		} else if(liuChunCheckTejiaZcStatus.equals("1")) {
			tejia_zc_jipiao_waiFangLocalAmount = String.valueOf(tejia_zc_jipiao_waifang_local_edit.getText());
			tejia_zc_jipiao_waiFangNonlocalAmount = String.valueOf(tejia_zc_jipiao_waifang_waidi_edit.getText());
		}
		System.out.println("bsp_jipiao_liuChunLocalPersent="+ bsp_jipiao_liuChunLocalPersent);
		System.out.println("bsp_jipiao_liuChunNonlocalPersent="+ bsp_jipiao_liuChunNonlocalPersent);
		System.out.println("bsp_jipiao_waiFangLocalAmount="+ bsp_jipiao_waiFangLocalAmount);
		System.out.println("bsp_jipiao_waiFangNonlocalAmount="+ bsp_jipiao_waiFangNonlocalAmount);
		
		System.out.println("b2b_benpiao_liuChunLocalPersent="+ b2b_benpiao_liuChunLocalPersent);
		System.out.println("b2b_benpiao_liuChunNonlocalPersent="+ b2b_benpiao_liuChunNonlocalPersent);
		System.out.println("b2b_benpiao_waiFangLocalAmount="+ b2b_benpiao_waiFangLocalAmount);
		System.out.println("b2b_benpiao_waiFangNonlocalAmount="+ b2b_benpiao_waiFangNonlocalAmount);
		
		System.out.println("tejia_zc_jipiao_liuChunLocalPersent="+ tejia_zc_jipiao_liuChunLocalPersent);
		System.out.println("tejia_zc_jipiao_liuChunNonlocalPersent="+ tejia_zc_jipiao_liuChunNonlocalPersent);
		System.out.println("tejia_zc_jipiao_waiFangLocalAmount="+ tejia_zc_jipiao_waiFangLocalAmount);
		System.out.println("tejia_zc_jipiao_waiFangNonlocalAmount="+ tejia_zc_jipiao_waiFangNonlocalAmount);
		
		String[] dd = bsp_jipiao_liuChunLocalPersent.split("\\|");
		System.out.println("dd size =  " + dd.length);
		
		System.out.println("bsp_jipiao_liuChunLocalPersent.length()="+ bsp_jipiao_liuChunLocalPersent.length());//11
		System.out.println("bsp_jipiao_liuChunLocalPersent.split().length()=" + (bsp_jipiao_liuChunLocalPersent.split("\\|")).length);
		
		
		if(bsp_jipiao_liuChunLocalPersent.split("\\|").length >1 && bsp_jipiao_liuChunLocalPersent.split("\\|").length < 4){
			isCheckRight = false;
			System.out.println("bsp_jipiao_liuChunLocalPersent isCheckRight="+ isCheckRight);
		}
		if(bsp_jipiao_liuChunNonlocalPersent.split("\\|").length >1 && bsp_jipiao_liuChunNonlocalPersent.split("\\|").length < 4){
			isCheckRight = false;
			System.out.println("bsp_jipiao_liuChunNonlocalPersent isCheckRight="+ isCheckRight);
		}
		
		if(b2b_benpiao_liuChunLocalPersent.split("\\|").length >1 && b2b_benpiao_liuChunLocalPersent.split("\\|").length < 4){
			isCheckRight = false;
			System.out.println("b2b_benpiao_liuChunLocalPersent isCheckRight="+ isCheckRight);
		}
		if(b2b_benpiao_liuChunNonlocalPersent.split("\\|").length >1 && b2b_benpiao_liuChunNonlocalPersent.split("\\|").length < 4){
			isCheckRight = false;
			System.out.println("b2b_benpiao_liuChunNonlocalPersent isCheckRight="+ isCheckRight);
		}
		
		if(tejia_zc_jipiao_liuChunLocalPersent.split("\\|").length >1 && tejia_zc_jipiao_liuChunLocalPersent.split("\\|").length < 4){
			isCheckRight = false;
			System.out.println("tejia_zc_jipiao_liuChunLocalPersent isCheckRight="+ isCheckRight);
		}
		if(tejia_zc_jipiao_liuChunNonlocalPersent.split("\\|").length >1 && tejia_zc_jipiao_liuChunNonlocalPersent.split("\\|").length < 4){
			isCheckRight = false;
			System.out.println("tejia_zc_jipiao_liuChunNonlocalPersent isCheckRight="+ isCheckRight);
		}
		
		System.out.println("isCheckRight="+ isCheckRight);
	}
	
	private void setListValue() {
		profitRuleList = new ArrayList<ProfitRule>();
		
		if(bsp_jipiao_liuChunLocalPersent.split("\\|").length == 4 && bsp_jipiao_liuChunNonlocalPersent.split("\\|").length == 4){
			ProfitRule bspProfitRule = new ProfitRule();
			bspProfitRule.setProfitMethod(liuChunCheckStatus);
			bspProfitRule.setServiceType("001");
			bspProfitRule.setDirOfficeID(dirOfficeID);
			bspProfitRule.setLocalPersent(bsp_jipiao_liuChunLocalPersent);
			bspProfitRule.setNonlocalPersent(bsp_jipiao_liuChunNonlocalPersent);
			bspProfitRule.setLocalAmount("");
			bspProfitRule.setNonlocalAmount("");
			profitRuleList.add(bspProfitRule);
		} else if(bsp_jipiao_waiFangLocalAmount.length() != 0 && bsp_jipiao_waiFangNonlocalAmount.length() != 0){
			ProfitRule bspProfitRule = new ProfitRule();
			bspProfitRule.setProfitMethod(liuChunCheckStatus);
			bspProfitRule.setServiceType("001");
			bspProfitRule.setDirOfficeID(dirOfficeID);
			bspProfitRule.setLocalPersent(bsp_jipiao_waiFangLocalAmount);
			bspProfitRule.setNonlocalPersent(bsp_jipiao_waiFangNonlocalAmount);
			bspProfitRule.setLocalAmount("");
			bspProfitRule.setNonlocalAmount("");
			profitRuleList.add(bspProfitRule);
		}
		
		if(b2b_benpiao_liuChunLocalPersent.split("\\|").length == 4 && b2b_benpiao_liuChunNonlocalPersent.split("\\|").length == 4){
			ProfitRule bspProfitRule = new ProfitRule();
			bspProfitRule.setProfitMethod(liuChunCheckTejiaStatus);
			bspProfitRule.setServiceType("003");
			bspProfitRule.setDirOfficeID(dirOfficeID);
			bspProfitRule.setLocalPersent(b2b_benpiao_liuChunLocalPersent);
			bspProfitRule.setNonlocalPersent(b2b_benpiao_liuChunNonlocalPersent);
			bspProfitRule.setLocalAmount("");
			bspProfitRule.setNonlocalAmount("");
			profitRuleList.add(bspProfitRule);
		} else if(b2b_benpiao_waiFangLocalAmount.length() != 0 && b2b_benpiao_waiFangNonlocalAmount.length() != 0){
			ProfitRule bspProfitRule = new ProfitRule();
			bspProfitRule.setProfitMethod(liuChunCheckTejiaStatus);
			bspProfitRule.setServiceType("003");
			bspProfitRule.setDirOfficeID(dirOfficeID);
			bspProfitRule.setLocalPersent(b2b_benpiao_waiFangLocalAmount);
			bspProfitRule.setNonlocalPersent(b2b_benpiao_waiFangNonlocalAmount);
			bspProfitRule.setLocalAmount("");
			bspProfitRule.setNonlocalAmount("");
			profitRuleList.add(bspProfitRule);
		}
		
		if(tejia_zc_jipiao_liuChunLocalPersent.split("\\|").length == 4 && tejia_zc_jipiao_liuChunNonlocalPersent.split("\\|").length == 4){
			ProfitRule bspProfitRule = new ProfitRule();
			bspProfitRule.setProfitMethod(liuChunCheckTejiaZcStatus);
			bspProfitRule.setServiceType("005");
			bspProfitRule.setDirOfficeID(dirOfficeID);
			bspProfitRule.setLocalPersent(tejia_zc_jipiao_liuChunLocalPersent);
			bspProfitRule.setNonlocalPersent(tejia_zc_jipiao_liuChunNonlocalPersent);
			bspProfitRule.setLocalAmount("");
			bspProfitRule.setNonlocalAmount("");
			profitRuleList.add(bspProfitRule);
		} else if(tejia_zc_jipiao_waiFangLocalAmount.length() != 0 && tejia_zc_jipiao_waiFangNonlocalAmount.length() != 0){
			ProfitRule bspProfitRule = new ProfitRule();
			bspProfitRule.setProfitMethod(liuChunCheckTejiaZcStatus);
			bspProfitRule.setServiceType("005");
			bspProfitRule.setDirOfficeID(dirOfficeID);
			bspProfitRule.setLocalPersent(tejia_zc_jipiao_waiFangLocalAmount);
			bspProfitRule.setNonlocalPersent(tejia_zc_jipiao_waiFangNonlocalAmount);
			bspProfitRule.setLocalAmount("");
			bspProfitRule.setNonlocalAmount("");
			profitRuleList.add(bspProfitRule);
		}
	}
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.lowerLevelButton:
//				Intent intent = new Intent();
//				intent.setClass(LowerLevelAdd.this, CityList.class);
//				startActivity(intent);
				saveOfficer();
				break;
			case R.id.statusBut:
				if(isCheck) {
					statusBut.setBackgroundResource(R.drawable.button_checked);
    		        status = "0";
    		        isCheck = false;
				} else {
					statusBut.setBackgroundResource(R.drawable.button_unchecked);
					status = "1";
					isCheck = true;
				}
				break;
			case R.id.bsp_jipiao_liuchun:
				if(isLiuChunCheck){
					bsp_jipiao_liuchun.setBackgroundResource(R.drawable.button_checked);
					bsp_jipiao_liuchun_marker.setVisibility(View.VISIBLE);
					bsp_jipiao_waifang_marker.setVisibility(View.GONE);
					bsp_jipiao_waifang.setBackgroundResource(R.drawable.button_unchecked);
					isLiuChunCheck = false;
					liuChunCheckStatus = "0";
				}else{
					bsp_jipiao_liuchun_marker.setVisibility(View.GONE);
					bsp_jipiao_waifang_marker.setVisibility(View.VISIBLE);
					bsp_jipiao_liuchun.setBackgroundResource(R.drawable.button_unchecked);
					bsp_jipiao_waifang.setBackgroundResource(R.drawable.button_checked);
					isLiuChunCheck = true;
					liuChunCheckStatus = "1";
				}
				break;
			case R.id.bsp_jipiao_waifang:
				if(isWaiFangCheck){
					bsp_jipiao_liuchun_marker.setVisibility(View.GONE);
					bsp_jipiao_waifang_marker.setVisibility(View.VISIBLE);
					bsp_jipiao_waifang.setBackgroundResource(R.drawable.button_checked);
					bsp_jipiao_liuchun.setBackgroundResource(R.drawable.button_unchecked);
					isWaiFangCheck = false;
					liuChunCheckStatus = "1";
				}else{
					bsp_jipiao_liuchun_marker.setVisibility(View.VISIBLE);
					bsp_jipiao_waifang_marker.setVisibility(View.GONE);
					bsp_jipiao_waifang.setBackgroundResource(R.drawable.button_unchecked);
					bsp_jipiao_liuchun.setBackgroundResource(R.drawable.button_checked);
					isWaiFangCheck = true;
					liuChunCheckStatus = "0";
				}
				break;
			case R.id.b2b_benpiao_jipiao_liuchun:
				if(isLiuChunCheckTejia){
					b2b_benpiao_jipiao_liuchun.setBackgroundResource(R.drawable.button_checked);
					b2b_benpiao_jipiao_liuchun_marker.setVisibility(View.VISIBLE);
					b2b_benpiao_jipiao_waifang_marker.setVisibility(View.GONE);
					b2b_benpiao_jipiao_waifang.setBackgroundResource(R.drawable.button_unchecked);
					isLiuChunCheckTejia = false;
					liuChunCheckTejiaStatus = "0";
				}else{
					b2b_benpiao_jipiao_liuchun_marker.setVisibility(View.GONE);
					b2b_benpiao_jipiao_waifang_marker.setVisibility(View.VISIBLE);
					b2b_benpiao_jipiao_liuchun.setBackgroundResource(R.drawable.button_unchecked);
					b2b_benpiao_jipiao_waifang.setBackgroundResource(R.drawable.button_checked);
					isLiuChunCheckTejia = true;
					liuChunCheckTejiaStatus = "1";
				}
				break;
			case R.id.b2b_benpiao_jipiao_waifang:
				if(isWaiFangCheckTejia){
					b2b_benpiao_jipiao_liuchun_marker.setVisibility(View.GONE);
					b2b_benpiao_jipiao_waifang_marker.setVisibility(View.VISIBLE);
					b2b_benpiao_jipiao_waifang.setBackgroundResource(R.drawable.button_checked);
					b2b_benpiao_jipiao_liuchun.setBackgroundResource(R.drawable.button_unchecked);
					isWaiFangCheckTejia = false;
					liuChunCheckTejiaStatus = "1";
				}else{
					b2b_benpiao_jipiao_liuchun_marker.setVisibility(View.VISIBLE);
					b2b_benpiao_jipiao_waifang_marker.setVisibility(View.GONE);
					b2b_benpiao_jipiao_waifang.setBackgroundResource(R.drawable.button_unchecked);
					b2b_benpiao_jipiao_liuchun.setBackgroundResource(R.drawable.button_checked);
					isWaiFangCheckTejia = true;
					liuChunCheckTejiaStatus = "0";
				}
				break;	
			case R.id.tejia_zc_jipiao_liuchun:
				if(isLiuChunCheckTejia){
					tejia_zc_jipiao_liuchun.setBackgroundResource(R.drawable.button_checked);
					tejia_zc_jipiao_liuchun_marker.setVisibility(View.VISIBLE);
					tejia_zc_jipiao_waifang_marker.setVisibility(View.GONE);
					tejia_zc_jipiao_waifang.setBackgroundResource(R.drawable.button_unchecked);
					isLiuChunCheckTejiaZc = false;
					liuChunCheckTejiaZcStatus = "0";
				}else{
					tejia_zc_jipiao_liuchun_marker.setVisibility(View.GONE);
					tejia_zc_jipiao_waifang_marker.setVisibility(View.VISIBLE);
					tejia_zc_jipiao_liuchun.setBackgroundResource(R.drawable.button_unchecked);
					tejia_zc_jipiao_waifang.setBackgroundResource(R.drawable.button_checked);
					isLiuChunCheckTejiaZc = true;
					liuChunCheckTejiaZcStatus = "1";
				}
				break;
			case R.id.tejia_zc_jipiao_waifang:
				if(isWaiFangCheckTejia){
					tejia_zc_jipiao_liuchun_marker.setVisibility(View.GONE);
					tejia_zc_jipiao_waifang_marker.setVisibility(View.VISIBLE);
					tejia_zc_jipiao_waifang.setBackgroundResource(R.drawable.button_checked);
					tejia_zc_jipiao_liuchun.setBackgroundResource(R.drawable.button_unchecked);
					isWaiFangCheckTejiaZc = false;
					liuChunCheckTejiaZcStatus = "1";
				}else{
					tejia_zc_jipiao_liuchun_marker.setVisibility(View.VISIBLE);
					tejia_zc_jipiao_waifang_marker.setVisibility(View.GONE);
					tejia_zc_jipiao_waifang.setBackgroundResource(R.drawable.button_unchecked);
					tejia_zc_jipiao_liuchun.setBackgroundResource(R.drawable.button_checked);
					isWaiFangCheckTejiaZc = true;
					liuChunCheckTejiaZcStatus = "0";
				}
				break;	
			case R.id.backBut: 
				Intent intentR = new Intent();
				intentR.setClass(LowerLevelTabAdd.this, LowerLevelQuery.class);
				startActivity(intentR);
				break;
		}
	}
	
	private void saveOfficer() {
		Log.i("", "保存分销商>>>>>>>>>>>>>>>>>>>");
		
		final Thread thread = new Thread() {
			public void run() {
				String officeNameV = distributor.getText().toString();
				String mobileV = mobile.getText().toString();
				String emailV = email.getText().toString();
//				String userNameV = administrator.getText().toString();
//				String passwordV = password.getText().toString(); || "".equals(userNameV) || "".equals(passwordV) || "".equals(profitAccountSelect)
				
				if ("".equals(officeNameV) || "".equals(mobileV) || "".equals(emailV)){
    				Message msg = new Message();
					msg.what = INPUT_ERROR;
					myHandler.sendMessage(msg);
    				return;
    			}
    			if(mobileV!=null && mobileV.length()>0){
					if (!mobileV.matches("[0-9]{11}")) {
						Message msg = new Message();
						msg.what = MOBILE_ERROR;
						myHandler.sendMessage(msg);
						return;
					}
    			}
    			if(emailV!=null && emailV.length()>0){
					if (!emailV.matches("\\w+@(\\w+\\.)+[a-z]{2,3}")) {
						Message msg = new Message();
						msg.what = EMAIL_ERROR;
						myHandler.sendMessage(msg);
						return;
					}
    			}
    			
    			checkEditValue();
    			if(!isCheckRight) {
    				Message msg = new Message();
					msg.what = SETVALUE_ERROR;
					myHandler.sendMessage(msg);
					return;
    			}
    			
    			progressDialog = ProgressDialog.show(LowerLevelTabAdd.this, "请稍等...", "保存分销商可能会\n占用比较长时间...",true);
    			
    			List<OfficeInfo> officeList = new ArrayList<OfficeInfo>();
    			OfficeInfo oInfo = new OfficeInfo();
    			oInfo.OfficeID = "";
    			oInfo.OfficeName = officeNameV;
    			oInfo.Email = emailV;
    			oInfo.Telephone = mobileV;
    			oInfo.Mobile = mobileV;
    			oInfo.OfficeState = "2";
//    			oInfo.setUserName(userNameV);
//    			oInfo.setPassword(passwordV);
    			oInfo.AllowAgent = "0";
//    			oInfo.setProfitAccount(profitAccountSelect);
    			
    			officeList.add(oInfo);
    			
//    			sp = getSharedPreferences("UserInfoB2B", Context.MODE_WORLD_WRITEABLE | Context.MODE_WORLD_READABLE);
//		        String userIdLogin = sp.getString("userIdB2B", null);
//		        String userPwd =  sp.getString("userPwdB2B", null);
//		        String asToken =   sp.getString("ASTokenB2B", null);
		        String userIdLogin = Constants.USERID;
		        String userPwd =  Constants.USERPWD;
		        String asToken =  Constants.ASToken;
		        
		        String serviceType = "INSERT";
		        OfficeResponseResult officeResponseResult = WebAccessor.officerRegister(userIdLogin,userPwd,asToken,serviceType,officeList);
		        
		        dirOfficeID = officeResponseResult.OfficeID;
		        setListValue();
		        ProfitRuleResult profitRuleResult = WebAccessor.profitRuleManage(userIdLogin,userPwd,asToken,serviceType,profitRuleList);
		        
		        if(officeResponseResult.Result.equals("0") && profitRuleResult.Result.equals("0")) {
		        	Message msg = new Message();
					msg.what = SUCCESS;
					myHandler.sendMessage(msg);
					progressDialog.dismiss();
					return;
		        }
		        progressDialog.setCancelable(true);
			}
		};
		
		
		thread.start();
	}
	
	public void onResume() {     
		super.onResume();     
		MobclickAgent.onResume(this); 
	}  
	
	public void onPause() {     
		super.onPause();     
		MobclickAgent.onPause(this); 
	}  
}
